package com.jzrh.flashbee.common.utils;

import com.jzrh.flashbee.common.core.constant.HttpStatusConstants;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Administrator-Jince
 * @since: 2023/11/10 14:47
 * @description: 返回结果统一封装
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;
    private String message;
    private T data;

    public Result(T data) {
        this.data = data;
    }

    public Result<T> setResult(Integer code, String message) {
        this.setCode(code);
        this.setMessage(message);
        return this;
    }

    /**
     * 操作成功
     *
     * @return Result<T> data
     */
    public static <T> Result<T> success() {
        return new Result<T>(null).setResult(HttpStatusConstants.SUCCESS, "操作成功");
    }

    /**
     * 操作成功
     *
     * @param message tips
     * @return Result<T> data
     */
    public static <T> Result<T> success(String message) {
        return new Result<T>(null).setResult(HttpStatusConstants.SUCCESS, message);
    }

    /**
     * 操作成功
     *
     * @param message tips
     * @param data    data
     * @return Result<T> data
     */
    public static <T> Result<T> success(String message, T data) {
        return new Result<T>(data).setResult(HttpStatusConstants.SUCCESS, message);
    }

    /**
     * 服务器错误
     *
     * @return Result<T> data
     */
    public static <T> Result<T> error() {
        return new Result<T>(null).setResult(HttpStatusConstants.ERROR, null);
    }

    /**
     * 服务器错误
     *
     * @param message tips
     * @return Result<T> data
     */
    public static <T> Result<T> error(String message) {
        return new Result<T>(null).setResult(HttpStatusConstants.ERROR, message);
    }

    /**
     * 自定义错误码
     * @param message tips
     * @param code 状态码
     * @return Result<T> data
     */
    public static <T> Result<T> error(String message, Integer code) {
        return new Result<T>(null).setResult(code, message);
    }

    /**
     * 参数列表错误（缺少，格式不匹配）
     *
     * @return Result<T> data
     */
    public static <T> Result<T> paramsError() {
        return new Result<T>(null).setResult(HttpStatusConstants.BAD_REQUEST, "参数列表错误，不匹配");
    }

    /**
     * 参数列表错误（缺少，格式不匹配）
     *
     * @return Result<T> data
     */
    public static <T> Result<T> paramsError(String message) {
        return new Result<T>(null).setResult(HttpStatusConstants.BAD_REQUEST, message);
    }

    /**
     * 未授权
     *
     * @param message tips
     * @return Result<T> data
     */
    public static <T> Result<T> unauthorized(String message) {
        return new Result<T>(null).setResult(HttpStatusConstants.UNAUTHORIZED, message);
    }

    /**
     * 系统警告
     *
     * @return Result<T> data
     */
    public static <T> Result<T> warn() {
        return new Result<T>(null).setResult(HttpStatusConstants.ERROR, "系统警告");
    }

    /**
     * 系统警告
     *
     * @param message tips
     * @return Result<T> data
     */
    public static <T> Result<T> warn(String message) {
        return new Result<T>(null).setResult(HttpStatusConstants.ERROR, message);
    }

    /**
     * 分页
     *
     * @param list  列表
     * @param total 总数
     * @param <T>
     * @return Map
     */
    public static <T> Result<Map<String, Object>> page(String message, List<T> list, Long total) {
        Map<String, Object> map = new HashMap<>();
        map.put("row", list);
        map.put("total", total);
        return new Result<>(map).setResult(HttpStatusConstants.SUCCESS, message);
    }
}
